<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first">
            <tree-tools :node-data="companyData" :is-show="true" style="border-bottom: 1px solid #ccc" />
            <!-- 树形控件 :data树形控件的数据 数组
             如果展示数据的节点和父子叶子依赖节点不是默认的label和children 可以手动修改配置项
             default-expand-all 默认展开所有节点

             插槽  默认插槽 具名插槽 作用域插槽
             -->
            <el-tree :data="departs" :props="defaultProps" default-expand-all>
              <!-- 结构出来的data就是树形控件要展示的数据 -->
              <template #default="{ data }">
                <tree-tools
                  :node-data="data"
                  @show-dialog="shouwAddDepts"
                  @del-depts="getDepartments"
                  @edit-depts="handleEditDepts"
                />
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 添加部门对话框组件 -->
      <add-depts
        ref="addDept"
        :dep-list="deptsList"
        :node-data="nodeData"
        :show-dialog.sync="showDialog"
        @add-depts="getDepartments"
        @closeDialog="handleClose"
      />
    </div>
  </div>
</template>

<script>
import { transformDepartments } from '@/utils/index'
import treeTools from './components/tree-tools'
import addDepts from './components/add-depts'
import { reqGetDepartments } from '@/api/department'
export default {
  name: 'Departments',
  components: { treeTools, addDepts },
  data() {
    return {
      activeName: 'first',
      // tree关联的数据源
      // 默认 label属性是页面展示的文本
      // 默认 children属性是叶子节点(父子节点)的关联关系字段
      departs: [],
      deptsList: [], // 列表数据
      nodeData: {},
      loading: false,
      // 修改默认配置项
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      companyData: {
        name: '',
        manager: ''
      },
      showDialog: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const res = await reqGetDepartments()
      console.log(res)
      this.departs = transformDepartments(res.data.depts, '')
      // 传递所有原始数据
      this.deptsList = res.data.depts
      console.log(this.departs)
      // 赋值标题的tree数据
      this.companyData.name = res.data.companyName
      this.companyData.manager = res.data.companyManage || '负责人'
      this.companyData.id = ''
      this.loading = false
    },
    handleClose() {
      this.showDialog = false
    },
    // 点击添加子部门按钮显示对话框接收数据
    shouwAddDepts(nodeData) {
      // 显示对话框
      this.showDialog = true
      // 接收数据
      this.nodeData = nodeData
      console.log(nodeData)
      // 调用子组件获取下拉列表的数的方法
      this.$refs.addDept.getEmployeeSimple()
    },
    // 编辑部门节点
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // 编辑状态同样也要获取部门负责人列表
      this.$refs.addDept.getEmployeeSimple()
      this.$refs.addDept.getDepartments(nodeData.id)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
  // 深度作用域
  ::v-deep {
    .el-tree-node__content {
      height: 32px;
    }
  }
  .el-tree {
  ::v-deep {
    // 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/smile.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
}
</style>
